/*
import {Component, Inject, OnInit, Optional} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {UtilityService} from '../../../service/utils.service';
import {ActivatedRoute, Router} from '@angular/router';
import {RoleModule} from '../../../service/role';
import {MenuItem, TreeNode} from 'primeng/api';
import {appConfig} from '../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import * as _ from 'lodash';
import {Observable} from 'rxjs/Observable';
import {CommondictServe} from '../../../service/commonDict';
import {ReuseTabService} from '@delon/abc';

@Component({
    selector: 'app-role',
    templateUrl: './role.component.html',
    styleUrls: ['./role.component.less']
})
export class RoleComponent implements OnInit {

    treedata: any[]; // tree组件数据
    treemenus: MenuItem[];
    searchTitle: string; // 树搜索框文字
    roleTitle: string; // 角色树标题
    tabShow: boolean;
    id: string;
    roleGroupVisible = false; // 弹出框默认关闭
    selectionType= 'single'
    role: RoleModule = new RoleModule();
    roleAdd: RoleModule = new RoleModule();
    selectIndex = 0; // tab的索引, 默认是0
    modelTitle: string;
    // 枚举值
    isEdit = false; // 默认是新增
    roleData: any; // 树节点上的数据保存
    isData = true; // 是否存在成员
    loading = false; // 是否存在成员

    saveType: string; // 保存类型
    enAbled: any; // 枚举值 是否
    // 角色分组
    roleGroup: any;
    dictGuid: string; // 业务字典guid
    roleName: string; //  角色名称
    roleInfo: any; // 角色信息
    isSlash  = false;
    rightRoleinfo: any; // 右击角色信息
    isroot: boolean; // 是否是角色组
    isShows: boolean;
    tabs =  [
        {
            name   : '角色成员',
            router : 'role/rolemember'
        },
        {
            name   : '功能权限',
            router : 'role/funcper'
        },{
            name   : '实体权限',
            router : 'role/entity'
        },
        {
            name   : '数据权限',
            router : 'role/field'
        },
        {
            name   : '字段范围',
            router : 'role/datarang'
        }
    ]
    constructor(
        private http: _HttpClient,
        private router: Router,
        private modal: NzModalService,
        private utilityService: UtilityService,
        private nznot: NzNotificationService,
        private commondict: CommondictServe,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        @Optional() @Inject(ReuseTabService) private reuseTabService: ReuseTabService,
    ) {
    }

    // 布局样式
    widthLeft = 7;
    widthRight = 17;
    isShow = true;
    text= '收起'

    // 自定义卡片收缩方法
    toggleCollapsed(): void {
        this.isShow = !this.isShow;
        if(this.isShow === true){
            this.widthLeft = 7;
            this.widthRight = 17;
            this.text = '收起'
        }else{
            this.widthLeft = 2;
            this.widthRight = 22;
            this.text = '展开'
        }
    }
    ngOnInit() {
        this.searchTitle = '请输入角色代码/名称';
        this.roleTitle = '角色详情'
        this.dictKeyQueryGuid();
        this.roleName = '角色管理';
        this.reuseTabService.title = '角色管理'
        if(this.reuseTabService.items) {
            if(this.reuseTabService.items.length > 0) {
                for(let i = 0; i < this.reuseTabService.items.length; i ++) {
                    console.log(this.reuseTabService.items[i])
                    if(this.reuseTabService.items[i].title !== '角色管理') { // 没有才添加
                        /!* 把当前路由添加到 tab 缓存中*!/
                        this.reuseTabService.store(this.activatedRoute.snapshot, '')
                    }
                }
            } else {
                /!* 把当前路由添加到 tab 缓存中*!/
                this.reuseTabService.store(this.activatedRoute.snapshot, '')
            }
        }
        this.tabShow = false;
        this.dictQueryKey();
    }



    // 根据key查询业务字典
    dictQueryKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.dictYon)
            .subscribe((val) => {
                this.enAbled = val.result;
            })

    }


    //  根据key查询业务字典guid
    dictKeyQueryGuid() {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.queryDictByKey + '/' + 'DICT_ROLE_GROUP')
            .subscribe(
                (val) => {
                    this.dictGuid = val.result.guid;
                    this.initData();
                }
            );
    }

    // 初始化角色数据
    initData() {
        this.treedata = [ // 默认根节点
            {
                'label': '角色列表',
                'data': 'default Role',
                'guid': 'root',
                'isleaf': 'true',
                'expandedIcon': 'anticon anticon-solution',
                'collapsedIcon': 'anticon anticon-solution',
                'children': [{}]
            }
        ];
        let objsJson = {
            name: ''
        }

        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.roleTreeFilter, objsJson)
            .subscribe(
                (val) => {
                    console.log(val.result)
                    for (var i = 0; i < val.result.length; i ++ ) {
                        val.result[i].collapsedIcon  = 'anticon anticon-team';
                        val.result[i].expandedIcon  = 'anticon anticon-team';
                        if(val.result[i].children) {
                            val.result[i].isleaf  = 'true';
                            val.result[i].children.collapsedIcon  = 'anticon anticon-team';
                            val.result[i].children.expandedIcon  = 'anticon anticon-team';
                            /!*this.getData(val.result[i])*!/
                            for(let s = 0; s < val.result[i].children.length; s++) {
                                if (val.result[i].children[s].enabled === '是') {
                                    val.result[i].children[s].collapsedIcon  = 'anticon anticon-user';
                                } else {
                                    val.result[i].children[s].collapsedIcon  = 'anticon anticon-user-delete';
                                }
                                val.result[i].children[s].label =  val.result[i].children[s].label + '-' + val.result[i].children[s].code;
                            }
                        }else {
                            val.result[i].isleaf  = 'false';
                            if(val.result[i].isleaf === 'false') {
                                console.log(val.result[i])
                                val.result[i].label  = val.result[i].label + '-' + val.result[i].code;
                            }
                            if (val.result[i].data === '是') {
                                val.result[i].collapsedIcon  = 'anticon anticon-user';
                            } else {
                                val.result[i].collapsedIcon  = 'anticon anticon-user-delete';
                            }
                        }
                    }
                    this.treedata[0].children = val.result;
                    this.treedata.forEach( node => {
                        this.expandRecursive(node, true);
                    });
                },
            );

    }


    // 展开所有树节点的方法
    private expandRecursive(node: TreeNode, isExpand:boolean){
        node.expanded = isExpand;
        if(node.children){
            node.children.forEach( childNode => {
                this.expandRecursive(childNode, isExpand);
            } );
        }
    }



    // 请求所有角色接口
    addGroupService() {
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.sysDictsTree + '/' +  this.dictGuid  , {})
            .subscribe(
                (val) => {
                    this.roleGroup = val.result.children;
                },
            );
    }


    getData(event) {
        let objJson = {
            roleGroup: event.label
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.queryRolesByGroup, objJson )
            .subscribe(
                (val) => {
                    for (let i = 0 ; i < val.result.length; i++) {
                        val.result[i].isleaf  = 'false';
                        if (val.result[i].enabled === '是') {
                            val.result[i].collapsedIcon  = 'anticon anticon-user';
                        } else {
                            val.result[i].collapsedIcon  = 'anticon anticon-user-delete';
                        }
                        val.result[i].label = val.result[i].roleCode + '-' + val.result[i].roleName;
                    }
                    event.children = val.result;
                });
    }


    // 展开节点的方法，请求方法
    Unfold(event) {
        if(event.node.guid !== 'root') { // 不是跟才请求
            // this.getData(event.node)
        }
    }


    memberdata = [

    ];
    // 左击树菜单节点信息
    TreeSelect(event) {
        this.id = event.node.guid;
        if (!event.node.children) {
            this.roleName = event.node.label;
            this.selectIndex = 0;
            this.router.navigate(['role/rolemember', this.id]); // 默认跳转角色路由
            this.tabShow = true;

            this.isroot = false; // 代表是角色
        } else {
            this.roleName = event.node.label;
            this.selectIndex = 0;
            this.router.navigate(['role/rolemember', 'root']); // 默认跳转角色路由

            this.tabShow = true;
            this.isroot = true; // 代表是角色组
        }
    }


    RightSelect(event) {
        if (event.node.guid === 'root') {
            this.treemenus = [
                {label: '新增角色组', icon: 'anticon anticon-usergroup-add', command: (event) => this.addRoleGroup()},
                {label: '新增角色', icon: 'anticon anticon-user-add', command: (event) => this.addRole('root')}
            ];
        } else {
            console.log(event.node)
            if (event.node.children) {
                this.roleInfo = event.node;
                if(event.node.itemName === '默认角色分组') { // 代表是默认角色分组，不允许删除
                    this.treemenus = [
                        {label: '新增角色', icon: 'anticon anticon-user-add', command: (event) => this.addRole('group')}]
                } else {
                    this.treemenus = [
                        {label: '修改角色组', icon: 'fa fa-edit', command: (event) => this.editRoleGroup()},
                        {label: '删除角色组', icon: 'anticon anticon-usergroup-delete', command: (event) => this.roleDel('root')},
                        {label: '新增角色', icon: 'anticon anticon-user-add', command: (event) => this.addRole('group')}]
                }


            } else {
                if(event.node.data === '是' || event.node.enabled === '是') {
                    this.treemenus = [
                        {label: '修改角色', icon: 'fa fa-edit', command: (event) => this.editRole()},
                        {label: '删除角色', icon: 'anticon anticon-user-delete', command: (event) => this.roleDel('child')},
                        {label: '关闭角色', icon: 'anticon anticon-frown', command: (event) => this.roleClose()},
                    ];
                } else {
                    this.treemenus = [
                        {label: '修改角色', icon: 'fa fa-edit', command: (event) => this.editRole()},
                        {label: '删除角色', icon: 'anticon anticon-user-delete', command: (event) => this.roleDel('child')},
                        {label: '启用角色', icon: 'anticon anticon-smile', command: (event) => this.roleOpen()},
                    ];
                }


            }
        }
        this.roleData = event.node; // 绑定右击方法数据

    }
    // 拖拽方法
    dropEvent($event) {
        console.log($event)
        if($event.dragNode.isleaf === 'false' && $event.dropNode.isleaf === 'true') { // 只能从角色移动到角色组，其他不可以
            let moveDate = {
                'guid': $event.dragNode.guid,
                'roleGroup': $event.dropNode.label,
            };
            if($event.dropNode.label === '角色列表'){
                moveDate.roleGroup = '';
            }
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.moveRole , moveDate )
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (err) => {
                        this.initData()
                        this.nznot.create('error', err.msg , err.msg);
                    }
                );
        } else {
            this.nznot.create('error', '移动错误' , '只能拖拽角色');
            this.initData();
        }
    }

    // 树节点搜索框的内容
    searchVal($event) {
        let jsonObj = {
            name: $event
        };
        if($event === ''){
            this.initData()
        }else {
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.roleTreeFilter, jsonObj)
                .subscribe(
                    (val) => {
                        console.log(val.result)
                        for (let i = 0; i < val.result.length; i ++ ) {
                                val.result[i].collapsedIcon  = 'anticon anticon-team';
                                val.result[i].expandedIcon  = 'anticon anticon-team';
                                if (val.result[i].children) {
                                    for( let s = 0 ; s < val.result[i].children.length; s ++) {
                                        val.result[i].children[s].isleaf  = 'true';
                                        val.result[i].children[s].collapsedIcon  = 'anticon anticon-user';
                                        val.result[i].children[s].expandedIcon  = 'anticon anticon-user';
                                    }
                                }else {
                                    val.result[i].isleaf  = 'false';
                                    if (val.result[i].data === '是') {
                                        val.result[i].collapsedIcon  = 'anticon anticon-user';
                                        val.result[i].expandedIcon  = 'anticon anticon-user';
                                    } else {
                                        val.result[i].collapsedIcon  = 'anticon anticon-user-delete';
                                    }
                                }
                        }
                        this.treedata[0].children = val.result;
                        this.treedata.forEach( node => {
                            this.expandRecursive(node, true);
                        });
                    },
                );
        }

    }

    // 树节点三个按钮方法
    clickFunc($event) {
        if($event === 'reset') {
            this.initData();
        } else if($event === 'expand') { // 全部展开方法
            let treeList = this.treedata[0].children
            for( let i = 0; i　< treeList.length; i++) {
                this.getData(treeList[i])
            }
            // 展开所有的
            this.treedata.forEach( node => {
                this.expandRecursive(node, true);
            } );
        }
    }


    // 根据id查询角色信息
    queryIdRole(item, edit?) {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.queryRoleById + '/' + item.guid)
            .subscribe(
                (val) => {
                    if(edit) {
                        if(val.result.enabled === '是') {
                            val.result.enabled = 'Y';
                        } else {
                            val.result.enabled = 'N';
                        }
                        this.roleAdd = val.result;
                        if(item.roleGroup) { // 如果存在
                            this.roleAdd.roleGroup = item.roleGroup; // 默认新增的角色组
                        } else {
                            this.roleAdd.roleGroup = ''
                        }

                    }

                },
                error => {

                });
    }

    // 根据id查询角色组信息
    queryIdRoleGroup(item) {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.sysDictItems + '/' + item.guid)
            .subscribe(
                (val) => {
                    this.roleAdd = val.result;
                    this.roleAdd.roleName = val.result.itemName;
                },
                error => {

                });
    }
    // 右击方法
    addRoleGroup() {
        this.roleAdd = new RoleModule();
        this.roleGroupVisible = true; // 打开弹窗
        this.modelTitle = '新增角色组'
        this.saveType = 'addroleGroup'
    }

    editRoleGroup() {
        this.roleAdd = new RoleModule();
        this.roleGroupVisible = true; // 打开弹窗
        this.modelTitle = '修改角色组';
        this.saveType = 'editroleGroup'
        this.queryIdRoleGroup(this.roleData);
    }


    addRole(item) {
        this.roleAdd = new RoleModule();
        this.roleGroupVisible = true; // 打开弹窗
        this.modelTitle = '新增角色';
        if(item === 'root') {
            this.roleAdd.roleGroup = ''; // 默认新增的角色组
        } else {

            this.roleAdd.roleGroup = this.roleInfo.label; // 默认新增的角色组
        }
        this.isSlash = false; // 默认不出现
        this.saveType = 'addRole';
        this.addGroupService()

    }

    // 枚举值转换
    isYon(event) {
        if (event.enabled === '是') {
            event.enabled = 'Y';
        } else if (event.enabled === '否') {
            event.enabled = 'N';
        }
    }
    // 修改角色
    editRole() {
        this.roleAdd = new RoleModule();
        this.modelTitle = '修改角色';
        this.saveType = 'editRole';
        this.queryIdRole(this.roleData, this.roleAdd);
        this.addGroupService()
        this.roleGroupVisible = true; // 打开弹窗
    }


    roleDel(item) {
        if(item === 'root') {
            this.modal.open({
                title: '是否删除',
                content: '您确认要删除【' + this.roleData.label + '】角色组吗? 删除分组后其下的角色会归入默认分组中',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.sysDicttems + '/' +  this.roleData.guid)
                        .subscribe(
                            (val) => {
                                this.initData();
                                this.nznot.create('success', val.msg , val.msg);
                                this.router.navigate(['role']); // 默认跳转角色路由
                                this.tabShow = false;
                            },
                            error=> {
                                this.nznot.create('error', error.msg , error.msg);
                            });
                },
                onCancel: () => {
                    console.log('失败');
                }
            });
        } else {
            this.modal.open({
                title: '是否删除',
                content: '您确认要删除【' + this.roleData.label + '】角色吗?',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.roleAdd + '/' +  this.roleData.guid)
                        .subscribe(
                            (val) => {
                           /!*     _.remove(this.roleData.parent.children, function(n: any) { // 前端伪删除
                                    return n.guid  === val.result.guid;
                                })*!/
                                this.initData();
                                this.nznot.create('success', val.msg , val.msg);
                                this.router.navigate(['role']); // 默认跳转角色路由
                                this.tabShow = false;
                            },
                            (error) => {
                                this.nznot.create('error', error.msg , error.msg);
                            });
                },
                onCancel: () => {
                    console.log('失败');
                }
            });
        }
    }

    // 保存接口
    save() {
        console.log(this.roleAdd)
        // 新增角色
        if (this.saveType === 'addRole') {
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.roleAdd , this.roleAdd)
                .subscribe(
                    (val) => {
                        console.log(val.code)
                        if (val.code !== '404' ) {
                            this.roleData.children.push( {
                                'label': val.result.roleName,
                                'data': 'addRole',
                                'guid': val.result.guid,
                                'expandedIcon': 'anticon anticon-user',
                                'collapsedIcon': 'anticon anticon-user',
                            })
                            this.nznot.create('success', val.msg , val.msg);
                        } else {
                            this.nznot.create('error', val.msg , val.msg);
                        }
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        }
        // 修改角色
        if (this.saveType === 'editRole') {
            let objJson = {
                guid: this.roleAdd.guid,
                roleName: this.roleAdd.roleName,
                roleCode: this.roleAdd.roleCode,
                roleDesc: this.roleAdd.roleDesc,
                roleGroup: this.roleAdd.roleGroup,
                enabled: this.roleAdd.enabled
            }
            this.utilityService.putData(appConfig.serverUrl + appConfig.API.roleEdit , objJson)
                .subscribe(
                    (val) => {
                        if(this.roleData.parent.label === '角色列表') {
                            this.initData()
                        } else {
                            this.getData(this.roleData.parent);
                        }
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        }

        // 新增角色组,调用业务字典的接口
        if (this.saveType === 'addroleGroup') {
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.addRoleGroup, this.roleAdd)
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }

        // 修改角色组
        if (this.saveType === 'editroleGroup') {

            this.utilityService.putData( appConfig.serverUrl  + appConfig.API.sysDictItems, this.roleAdd)
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        }

        this.roleGroupVisible = false;
    }


    // 360切换
    getRouter(it) {
        console.log(it)
        if (this.isroot) { // 代表是角色组
            this.selectIndex = it.index;
            this.router.navigate([this.tabs[it.index].router, 'root']);
        } else {
            this.selectIndex = it.index;
            this.router.navigate([this.tabs[it.index].router, this.id]);
        }
    }


    // 停用角色
    roleClose() {
        this.utilityService.putData( appConfig.serverUrl  + appConfig.API.disableRole + '/' + this.roleData.guid)
            .subscribe(
                (val) => {
                    console.log(this.roleData)
                   this.initData()
                    this.nznot.create('success', val.msg , val.msg);
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                });
    }
    // 启用角色
    roleOpen() {
        this.utilityService.putData( appConfig.serverUrl  + appConfig.API.enableRole + '/' + this.roleData.guid)
            .subscribe(
                (val) => {
                    /!*this.roleData.data = val.result.enabled; // 状态修改
                    val.roleData.collapsedIcon  = 'anticon anticon-user';*!/
                    this.initData()
                    this.nznot.create('success', val.msg , val.msg);
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                });
    }




    roleCodeexit(item) {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.existRoleCode + '/' + item)
            .subscribe(
                (sus) => {
                    console.log(sus)
                    if (sus.result) {
                        this.isSlash = true; // 重复
                    } else {
                        this.isSlash = false; // 不重复
                    }
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }

    empCodeexit(item) {
        let objData = {
            "itemValue": item
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.existItemValue, objData)
            .subscribe(
                (sus) => {
                    if (sus.result) {
                        this.isSlash = true; // 重复
                    } else {
                        this.isSlash = false; // 不重复
                    }
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }

}
*/


/*逻辑重写*/
import {Component, Inject, OnInit, Optional} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {UtilityService} from '../../../service/utils.service';
import {ActivatedRoute, Router} from '@angular/router';
import {RoleModule} from '../../../service/role';
import {MenuItem, TreeNode} from 'primeng/api';
import {appConfig} from '../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import * as _ from 'lodash';
import {Observable} from 'rxjs/Observable';
import {CommondictServe} from '../../../service/commonDict';
import {ReuseTabService} from '@delon/abc';
import {OperaRoleModule} from '../../../service/common.module';

@Component({
    selector: 'app-role',
    templateUrl: './role.component.html',
    styleUrls: ['./role.component.less']
})
export class RoleComponent implements OnInit {

    treedata: any[]; // tree组件数据
    treemenus: MenuItem[];
    searchTitle: string; // 树搜索框文字
    roleTitle: string; // 角色树标题
    tabShow: boolean;
    id: string;
    roleGroupVisible = false; // 弹出框默认关闭
    selectionType= 'single'
    role: RoleModule = new RoleModule();
    roleAdd: RoleModule = new RoleModule();
    selectIndex = 0; // tab的索引, 默认是0
    modelTitle: string;
    // 枚举值
    isEdit = false; // 默认是新增
    roleData: any; // 树节点上的数据保存
    isData = true; // 是否存在成员
    loading = false; // 是否存在成员

    saveType: string; // 保存类型
    enAbled: any; // 枚举值 是否
    // 角色分组
    roleGroup: any;
    dictGuid: string; // 业务字典guid
    roleName: string; //  角色名称
    roleInfo: any; // 角色信息
    isSlash  = false;
    rightRoleinfo: any; // 右击角色信息
    isroot: boolean; // 是否是角色组
    isShows: boolean;
    tabs =  [
        {
            name   : '角色成员',
            router : 'role/rolemember',
            id: 'rolemember',
        },
        {
            name   : '功能权限',
            router : 'role/funcper',
            id: 'funcper',
        },{
            name   : '实体权限',
            router : 'role/entity',
            id: 'entity',
        },
        {
            name   : '数据权限',
            router : 'role/field',
            id: 'field',
        },
        {
            name   : '字段范围',
            router : 'role/datarang',
            id: 'datarang',
        }
    ]
    appcofList: any;
    constructor(
        private http: _HttpClient,
        private router: Router,
        private modal: NzModalService,
        private utilityService: UtilityService,
        private nznot: NzNotificationService,
        private commondict: CommondictServe,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        @Optional() @Inject(ReuseTabService) private reuseTabService: ReuseTabService,
    ) {
    }

    // 布局样式
    widthLeft = 7;
    widthRight = 17;
    isShow = true;
    text= '收起'

    // 自定义卡片收缩方法
    toggleCollapsed(): void {
        this.isShow = !this.isShow;
        if(this.isShow === true){
            this.widthLeft = 7;
            this.widthRight = 17;
            this.text = '收起'
        }else{
            this.widthLeft = 2;
            this.widthRight = 22;
            this.text = '展开'
        }
    }
    ngOnInit() {
        this.appcofList = appConfig;
        this.searchTitle = '请输入角色代码/名称';
        this.roleTitle = '角色详情'
        this.dictKeyQueryGuid();
        this.roleName = '角色管理';
        this.reuseTabService.title = '角色管理'
        this.tabShow = false;
        this.dictQueryKey();
        // 调用角色功能查询接口
    }



    // 根据key查询业务字典
    dictQueryKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.dictYon)
            .subscribe((val) => {
                this.enAbled = val.result;
            })

    }


    //  根据key查询业务字典guid
    dictKeyQueryGuid() {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.queryDictByKey + '/' + 'DICT_ROLE_GROUP')
            .subscribe(
                (val) => {
                    this.dictGuid = val.result.guid;
                    this.initData();
                }
            );
    }

    // 初始化角色数据
    initData() {
        this.treedata = [ // 默认根节点
            {
                'label': '角色列表',
                'data': 'default Role',
                'guid': 'root',
                'isleaf': 'true',
                'expandedIcon': 'anticon anticon-solution',
                'collapsedIcon': 'anticon anticon-solution',
                'children': [{}]
            }
        ];
        let objsJson = {
            name: ''
        }

        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.roleTreeFilter, objsJson)
            .subscribe(
                (val) => {
                    for (var i = 0; i < val.result.length; i ++ ) {
                        val.result[i].collapsedIcon  = 'anticon anticon-team';
                        val.result[i].expandedIcon  = 'anticon anticon-team';
                        if(val.result[i].children) {
                            val.result[i].isleaf  = 'true';
                            val.result[i].children.collapsedIcon  = 'anticon anticon-team';
                            val.result[i].children.expandedIcon  = 'anticon anticon-team';
                            /*this.getData(val.result[i])*/
                            for(let s = 0; s < val.result[i].children.length; s++) {
                                if (val.result[i].children[s].enabled === '是') {
                                    val.result[i].children[s].collapsedIcon  = 'anticon anticon-user';
                                } else {
                                    val.result[i].children[s].collapsedIcon  = 'anticon anticon-user-delete';
                                }
                                val.result[i].children[s].label =  val.result[i].children[s].label + '-' + val.result[i].children[s].code;
                            }
                        }else {
                            val.result[i].isleaf  = 'false';
                            if(val.result[i].isleaf === 'false') {
                                val.result[i].label  = val.result[i].label + '-' + val.result[i].code;
                            }
                            if (val.result[i].data === '是') {
                                val.result[i].collapsedIcon  = 'anticon anticon-user';
                            } else {
                                val.result[i].collapsedIcon  = 'anticon anticon-user-delete';
                            }
                        }
                    }
                    this.treedata[0].children = val.result;
                    this.treedata.forEach( node => {
                        this.expandRecursive(node, true);
                    });
                },
            );

    }


    // 展开所有树节点的方法
    private expandRecursive(node: TreeNode, isExpand:boolean){
        node.expanded = isExpand;
        if(node.children){
            node.children.forEach( childNode => {
                this.expandRecursive(childNode, isExpand);
            } );
        }
    }



    // 请求所有角色接口
    addGroupService() {
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.sysDictsTree + '/' +  this.dictGuid  , {})
            .subscribe(
                (val) => {
                    this.roleGroup = val.result.children;
                },
            );
    }


    getData(event) {
        let objJson = {
            roleGroup: event.label
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.queryRolesByGroup, objJson )
            .subscribe(
                (val) => {
                    for (let i = 0 ; i < val.result.length; i++) {
                        val.result[i].isleaf  = 'false';
                        if (val.result[i].enabled === '是') {
                            val.result[i].collapsedIcon  = 'anticon anticon-user';
                        } else {
                            val.result[i].collapsedIcon  = 'anticon anticon-user-delete';
                        }
                        val.result[i].label = val.result[i].roleCode + '-' + val.result[i].roleName;
                    }
                    event.children = val.result;
                });
    }


    // 展开节点的方法，请求方法
    Unfold(event) {
        if(event.node.guid !== 'root') { // 不是跟才请求
            // this.getData(event.node)
        }
    }


    memberdata = [

    ];
    // 左击树菜单节点信息
    TreeSelect(event) {
        this.id = event.node.guid;
        if (!event.node.children) {
            this.roleName = event.node.label;
            this.selectIndex = 0;
            this.tabShow = true;
            this.tabId = 'rolemember'; // 默认角色员工
            this.isroot = false; // 代表是角色
            /*初始化角色信息*/
            this.initroleEmp();
        } else {
            this.roleName = event.node.label;
            this.selectIndex = 0;
            this.tabShow = false;
            this.isroot = true; // 代表是角色组
        }
    }


    RightSelect(event) {
        if (event.node.guid === 'root') {
            this.treemenus = [
                {label: '新增角色组', icon: 'anticon anticon-usergroup-add', id: 'Opr006001', command: (event) => this.addRoleGroup()},
                {label: '新增角色', icon: 'anticon anticon-user-add', id: 'Opr006003',command: (event) => this.addRole('root')}
            ];
        } else {
            if (event.node.children) {
                this.roleInfo = event.node;
                if(event.node.itemName === '默认角色分组') { // 代表是默认角色分组，不允许删除
                    this.treemenus = [
                        {label: '新增角色', icon: 'anticon anticon-user-add',id: 'Opr006003', command: (event) => this.addRole('group')}]
                } else {
                    this.treemenus = [
                        {label: '修改角色组', icon: 'fa fa-edit', id: 'Opr007011',command: (event) => this.editRoleGroup()},
                        {label: '删除角色组', icon: 'anticon anticon-usergroup-delete', id: 'Opr007012',command: (event) => this.roleDel('root')},
                        {label: '新增角色', icon: 'anticon anticon-user-add', id: 'Opr006003', command: (event) => this.addRole('group')}]
                }


            } else {
                if(event.node.data === '是' || event.node.enabled === '是') {
                    this.treemenus = [
                        {label: '修改角色', icon: 'fa fa-edit', id: 'Opr006004',command: (event) => this.editRole()},
                        {label: '删除角色', icon: 'anticon anticon-user-delete', id: 'Opr006005',command: (event) => this.roleDel('child')},
                        {label: '关闭角色', icon: 'anticon anticon-frown',id: 'Opr006006', command: (event) => this.roleClose()},
                    ];
                } else {
                    this.treemenus = [
                        {label: '修改角色', icon: 'fa fa-edit', id: 'Opr006004',command: (event) => this.editRole()},
                        {label: '删除角色', icon: 'anticon anticon-user-delete', id: 'Opr006005',command: (event) => this.roleDel('child')},
                        {label: '启用角色', icon: 'anticon anticon-smile',id: 'Opr006007',  command: (event) => this.roleOpen()},
                    ];
                }


            }
        }
        this.roleData = event.node; // 绑定右击方法数据

    }
    // 拖拽方法
    dropEvent($event) {
        if($event.dragNode.isleaf === 'false' && $event.dropNode.isleaf === 'true') { // 只能从角色移动到角色组，其他不可以
            let moveDate = {
                'guid': $event.dragNode.guid,
                'roleGroup': $event.dropNode.label,
            };
            if($event.dropNode.label === '角色列表'){
                moveDate.roleGroup = '';
            }
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.moveRole , moveDate )
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (err) => {
                        this.initData()
                        this.nznot.create('error', err.msg , err.msg);
                    }
                );
        } else {
            this.nznot.create('error', '移动错误' , '只能拖拽角色');
            this.initData();
        }
    }

    // 树节点搜索框的内容
    searchVal($event) {
        let jsonObj = {
            name: $event
        };
        if($event === ''){
            this.initData()
        }else {
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.roleTreeFilter, jsonObj)
                .subscribe(
                    (val) => {
                        for (let i = 0; i < val.result.length; i ++ ) {
                            val.result[i].collapsedIcon  = 'anticon anticon-team';
                            val.result[i].expandedIcon  = 'anticon anticon-team';
                            if (val.result[i].children) {
                                for( let s = 0 ; s < val.result[i].children.length; s ++) {
                                    val.result[i].children[s].isleaf  = 'true';
                                    val.result[i].children[s].collapsedIcon  = 'anticon anticon-user';
                                    val.result[i].children[s].expandedIcon  = 'anticon anticon-user';
                                }
                            }else {
                                val.result[i].isleaf  = 'false';
                                if (val.result[i].data === '是') {
                                    val.result[i].collapsedIcon  = 'anticon anticon-user';
                                    val.result[i].expandedIcon  = 'anticon anticon-user';
                                } else {
                                    val.result[i].collapsedIcon  = 'anticon anticon-user-delete';
                                }
                            }
                        }
                        this.treedata[0].children = val.result;
                        this.treedata.forEach( node => {
                            this.expandRecursive(node, true);
                        });
                    },
                );
        }

    }

    // 树节点三个按钮方法
    clickFunc($event) {
        if($event === 'reset') {
            this.initData();
        } else if($event === 'expand') { // 全部展开方法
            let treeList = this.treedata[0].children
            for( let i = 0; i　< treeList.length; i++) {
                this.getData(treeList[i])
            }
            // 展开所有的
            this.treedata.forEach( node => {
                this.expandRecursive(node, true);
            } );
        }
    }


    // 根据id查询角色信息
    queryIdRole(item, edit?) {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.queryRoleById + '/' + item.guid)
            .subscribe(
                (val) => {
                    if(edit) {
                        if(val.result.enabled === '是') {
                            val.result.enabled = 'Y';
                        } else {
                            val.result.enabled = 'N';
                        }
                        this.roleAdd = val.result;
                        if(item.roleGroup) { // 如果存在
                            this.roleAdd.roleGroup = item.roleGroup; // 默认新增的角色组
                        } else {
                            this.roleAdd.roleGroup = ''
                        }

                    }

                },
                error => {

                });
    }

    // 根据id查询角色组信息
    queryIdRoleGroup(item) {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.sysDictItems + '/' + item.guid)
            .subscribe(
                (val) => {
                    this.roleAdd = val.result;
                    this.roleAdd.roleName = val.result.itemName;
                },
                error => {

                });
    }
    // 右击方法
    addRoleGroup() {
        this.roleAdd = new RoleModule();
        this.roleGroupVisible = true; // 打开弹窗
        this.modelTitle = '新增角色组'
        this.saveType = 'addroleGroup'
    }

    editRoleGroup() {
        this.roleAdd = new RoleModule();
        this.roleGroupVisible = true; // 打开弹窗
        this.modelTitle = '修改角色组';
        this.saveType = 'editroleGroup'
        this.queryIdRoleGroup(this.roleData);
    }


    addRole(item) {
        this.roleAdd = new RoleModule();
        this.roleGroupVisible = true; // 打开弹窗
        this.modelTitle = '新增角色';
        if(item === 'root') {
            this.roleAdd.roleGroup = ''; // 默认新增的角色组
        } else {

            this.roleAdd.roleGroup = this.roleInfo.label; // 默认新增的角色组
        }
        this.isSlash = false; // 默认不出现
        this.saveType = 'addRole';
        this.addGroupService()

    }

    // 枚举值转换
    isYon(event) {
        if (event.enabled === '是') {
            event.enabled = 'Y';
        } else if (event.enabled === '否') {
            event.enabled = 'N';
        }
    }
    // 修改角色
    editRole() {
        this.roleAdd = new RoleModule();
        this.modelTitle = '修改角色';
        this.saveType = 'editRole';
        this.queryIdRole(this.roleData, this.roleAdd);
        this.addGroupService()
        this.roleGroupVisible = true; // 打开弹窗
    }


    roleDel(item) {
        if(item === 'root') {
            this.modal.open({
                title: '是否删除',
                content: '您确认要删除【' + this.roleData.label + '】角色组吗? 删除分组后其下的角色会归入默认分组中',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.sysDicttems + '/' +  this.roleData.guid)
                        .subscribe(
                            (val) => {
                                this.initData();
                                this.nznot.create('success', val.msg , val.msg);
                                this.router.navigate(['role']); // 默认跳转角色路由
                                this.tabShow = false;
                            },
                            error=> {
                                this.nznot.create('error', error.msg , error.msg);
                            });
                },
                onCancel: () => {
                }
            });
        } else {
            this.modal.open({
                title: '是否删除',
                content: '您确认要删除【' + this.roleData.label + '】角色吗?',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.roleAdd + '/' +  this.roleData.guid)
                        .subscribe(
                            (val) => {
                                /*     _.remove(this.roleData.parent.children, function(n: any) { // 前端伪删除
                                         return n.guid  === val.result.guid;
                                     })*/
                                this.initData();
                                this.nznot.create('success', val.msg , val.msg);
                                this.router.navigate(['role']); // 默认跳转角色路由
                                this.tabShow = false;
                            },
                            (error) => {
                                this.nznot.create('error', error.msg , error.msg);
                            });
                },
                onCancel: () => {
                }
            });
        }
    }

    // 保存接口
    save() {
        // 新增角色
        if (this.saveType === 'addRole') {
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.roleAdd , this.roleAdd)
                .subscribe(
                    (val) => {
                        if (val.code !== '404' ) {
                            this.roleData.children.push( {
                                'label': val.result.roleName,
                                'data': 'addRole',
                                'guid': val.result.guid,
                                'expandedIcon': 'anticon anticon-user',
                                'collapsedIcon': 'anticon anticon-user',
                            })
                            this.nznot.create('success', val.msg , val.msg);
                        } else {
                            this.nznot.create('error', val.msg , val.msg);
                        }
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        }
        // 修改角色
        if (this.saveType === 'editRole') {
            let objJson = {
                guid: this.roleAdd.guid,
                roleName: this.roleAdd.roleName,
                roleCode: this.roleAdd.roleCode,
                roleDesc: this.roleAdd.roleDesc,
                roleGroup: this.roleAdd.roleGroup,
                enabled: this.roleAdd.enabled
            }
            this.utilityService.putData(appConfig.serverUrl + appConfig.API.roleEdit , objJson)
                .subscribe(
                    (val) => {
                        if(this.roleData.parent.label === '角色列表') {
                            this.initData()
                        } else {
                            this.getData(this.roleData.parent);
                        }
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        }

        // 新增角色组,调用业务字典的接口
        if (this.saveType === 'addroleGroup') {
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.addRoleGroup, this.roleAdd)
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }

        // 修改角色组
        if (this.saveType === 'editroleGroup') {

            this.utilityService.putData( appConfig.serverUrl  + appConfig.API.sysDictItems, this.roleAdd)
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        }

        this.roleGroupVisible = false;
    }

    tabId: string; // 360跳转路由
    // 360切换
    getRouter(it) {
        this.selectIndex = it.index;
        if(it.index === 0) {
            this.tabId = 'rolemember'
            this.initroleEmp();
        } else if(it.index === 1) {
            this.tabId = 'funcper'
            this.InitRoleFunc()
        } else if(it.index === 2) {

            this.tabId = 'entity'

        } else if(it.index === 3) {
            this.tabId = 'field';
        } else {
            this.tabId = 'datarang'
        }
    }


    // 停用角色
    roleClose() {
        this.utilityService.putData( appConfig.serverUrl  + appConfig.API.disableRole + '/' + this.roleData.guid)
            .subscribe(
                (val) => {
                    this.initData()
                    this.nznot.create('success', val.msg , val.msg);
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                });
    }
    // 启用角色
    roleOpen() {
        this.utilityService.putData( appConfig.serverUrl  + appConfig.API.enableRole + '/' + this.roleData.guid)
            .subscribe(
                (val) => {
                    /*this.roleData.data = val.result.enabled; // 状态修改
                    val.roleData.collapsedIcon  = 'anticon anticon-user';*/
                    this.initData()
                    this.nznot.create('success', val.msg , val.msg);
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                });
    }




    roleCodeexit(item) {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.existRoleCode + '/' + item)
            .subscribe(
                (sus) => {
                    if (sus.result) {
                        this.isSlash = true; // 重复
                    } else {
                        this.isSlash = false; // 不重复
                    }
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }

    empCodeexit(item) {
        let objData = {
            "itemValue": item
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.existItemValue, objData)
            .subscribe(
                (sus) => {
                    if (sus.result) {
                        this.isSlash = true; // 重复
                    } else {
                        this.isSlash = false; // 不重复
                    }
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }
























    /*---------------------------角色下员工---------------------------------------*/
    operators: OperaRoleModule = new OperaRoleModule(); // 绑定数据
    roleGuid: string;
    buttons = [
        {key: 'add', value: '分配成员', buttonCode: 'Opr006009'}
    ]
    configTitle: any; // 操作按钮名称
    total: number; // 总页数
    selectType: string; // 选择员工类型
    orgCode: string; // 组织机构字符串
    orgList: any; // 组织机构列表
    pageIndex = 1; // 当前页数
    roleType: any; //　员工状态
    operType: any; //　操作员状态
    selectedRows: any; // 总数
    modalVisible = false;
    page: any; // 翻页数据
    orgChildList: any;
    data: any[] = []; // 表格数据
    headerData = [  // 配置表头内容
        {value: '操作员姓名', key: 'operatorName',  isclick: false},
        {value: '登录用户名' , key: 'userId', isclick: false},
        {value: '操作员状态' , key: 'operatorStatus', isclick: false}
    ];

    assignRole: string; // 分配角色标题
    moreData = { morebutton: true,
        buttons: [
            {key: 'Overview' , value: '查看概况'},
            {key: 'Authority' , value: '权限配置'}
        ]
    }

    test: string;
    isNull: boolean; // 是否存在角色成员
    roleAddModal = false; // 新增弹框
    list: any[] = []; // 角色列表
    orgCodeChild: string;
    isrootChild: boolean; // 判断是角色还是角色组
    initroleEmp() {
        this.roleGuid = this.id; // 拿到父组件传过来的组织机构的guid来进行操作
        if(this.isroot) {// 代表是不存在的
            this.isrootChild = false;
        } else {
            this.isrootChild = true;
        }
        this.getRoleEmpData(); // 只会触发一次，但是ngchanges并不会触发咋办
        this.dictRoleEmpQueryKey()
        this.configTitle = { value: '修改',  buttonCode: 'OprNo'}
    }


    // 根据key查询业务字典
    dictRoleEmpQueryKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.operatorStatus)
            .subscribe((val) => {
                this.operType = val.result;
            })

        this.commondict.queryDictKey(appConfig.dictKeyList.empStatus)
            .subscribe((val) => {
                this.roleType = val.result;
            })
    }




    getRoleEmpData(options?) { // 初始化请求后台数据
        if (options) {
            this.pageIndex = 1;
        }
        this.page = {
            condition: this.objJson, // 搜索内容
            page: {
                current: this.pageIndex,
                size: this.operators.size,
                orderByField: 'guid',
                asc: false // asc 默认是true  升序排序，时间类型 用false， 降序
            }
        };
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.queryByRole + '/' + this.roleGuid , this.page)
            .subscribe(
                (val) => {
                    for (let i = 0; i <  val.result.records.length; i++) {
                        val.result.records[i].buttonData = [ {key: 'dels', value: '删除', if: false, buttonCode: 'Opr006010'}];
                    }
                    this.data = val.result.records;
                    this.total = val.result.total;
                },
                error => {
                    this.nznot.create('error', error.code , error.msg);
                }
            );
        for(var i = 0; i < this.data.length; i ++) {
            this.data[i].buttonData = [ {key: 'dels', value: '删除', if: false}]
        }

    }

    // 想一下，能否把这三个方法封装到一个ts里面，引入即可，不然每次都写着三个方法不太现实。
    // 列表组件传过来的内容
    addHandler(event) {
        if (event === 'add') {
            this.roleAddModal = true;  // 此时点击了列表组件的新增，打开模态框
            this.selectArray = [];
            this.selectType = 'empName'
            this.assignRole = '为角色【' + this.roleName + '】分配操作员'
            this.getroleList()
        } else { // 代表修改，把修改的内容传递进去，重新渲染
            this.selectArray = [];
            this.roleAddModal = true;  // 此时点击了列表组件的新增，打开模态框
            this.assignRole = '为角色【' + this.roleName + '】分配操作员'
        }
    }

    // select更改方法
    selectChange(item) {
        this.orgChildList = [];
        this.orgCodeChild = '';
        if(item === 'org') {
            this.orgCode = '' ; // 清空
            // 查询所有机构
            this.utilityService.getData(appConfig.serverUrl  + appConfig.API.cascadeQueryAll)
                .subscribe(
                    (val) => {
                        this.orgList = val.result;
                    });
        } else {
            // 请求所有的员工
            this.getroleList();
        }
    }

    selectorgorgChange(guid) {
        for( let i = 0; i < this.orgList.length; i ++ ) {
            if(guid === this.orgList[i].guid) {
                this.orgChildList = this.orgList[i].children;
            }
        }
    }

    // 组织机构进行修改选择方法
    selectorgChange(guid) {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.queryEmployeesByOrgId + '/' + guid + '/' + this.roleGuid)
            .subscribe(
                (val) => {
                    const ret = [];
                    for (let i = 0; i < val.result.length; i++) {
                        ret.push({
                            key: i.toString(),
                            title:val.result[i]['empName'], // 名称g
                            userId:val.result[i]['userId'], // 操作员userId
                            guid:val.result[i]['guidOperator'], // 操作员guid
                            status: val.result[i]['exit'], // 状态，用来保存给后台的标识
                            description: val.result[i]['empName'],  // 描述
                            direction: val.result[i]['exit'] === 'true' ? 'left' : 'right', // 在左边还是右边，协商的字段 exit，给true代表左边未分配的，false代表右边，已分配的
                        });
                    }
                    this.list = ret;
                });
        /*   .subscribe(
               (val) => {

               });*/
    }

    // 根据父机构查询子机构
    selectorgchildChange(guid) {
        // 请求写在这里即可
    }

    // 右侧操作删除按钮
    buttonEvent(event) {
        if(event.names.key === 'dels') {
            this.modal.open({
                title: '是否删除',
                content: '您确认要删除该操作员吗',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    // 换接口即可
                    let deletaObj = {
                        operatorIdList: [event.guid],
                        roleIdList: [this.roleGuid]
                    }
                    this.utilityService.postData(appConfig.serverUrl + appConfig.API.batchDelete, deletaObj )
                        .subscribe(
                            (val) => {
                                this.getRoleEmpData()
                                this.nznot.create('success', val.msg , val.msg);
                            },
                            (error)=> {
                                this.nznot.create('error', error.msg , error.msg);
                            });

                },
                onCancel: () => {
                }
            });
        }
    }

    // 批量删除
    deleteBatch(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '个操作员吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    operatorIdList:delarray ,
                    roleIdList: [this.roleGuid]
                }
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.batchDelete, deletaObj )
                    .subscribe(
                        (val) => {
                            this.getRoleEmpData();
                            this.selectedRows = []
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error)=> {
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                this.selectedRows = []
                this.getRoleEmpData()
            }
        });

    }



    // 工程穿梭框
    copyList = []; // 拷贝数组
    getroleList(){
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.queryEntryEmp + '/' + this.roleGuid)
            .subscribe(
                (val) => {
                    const ret = [];
                    for (let i = 0; i < val.result.length; i++) {
                        ret.push({
                            key: i.toString(),
                            title:val.result[i]['empName'], // 名称g
                            userId:val.result[i]['userId'], // 操作员userId
                            guid:val.result[i]['guidOperator'], //操作员guid
                            status: val.result[i]['exit'], // 状态，用来保存给后台的标识
                            description: val.result[i]['empName'],  // 描述
                            direction: val.result[i]['exit'] === 'true' ? 'left' : 'right', // 在左边还是右边，协商的字段 exit，给true代表左边未分配的，false代表右边，已分配的
                        });
                    }
                    localStorage.setItem('copy', JSON.stringify(ret))
                    this.list = ret;
                });
    }


    subPro = [];
    selectArray  = []; // 默认是空
    selecstArray  = []; // 默认是空
    change(ret: any) {
        for(let i = 0 ; i < ret.list.length; i++){
            ret.list[i]['status'] =  ret.to
            ret.list[i]['direction'] =  ret.to
        }
    }

    //新增提交
    subAddrole(){
        this.copyList = JSON.parse(localStorage.getItem('copy')); // 后台返回状态
        for(let i = 0; i < this.list.length; i++) {
            for(let s = 0; s < this.copyList.length; s++) {
                if(this.list[i].userId === this.copyList[s].userId) {
                    if(this.list[i].direction != this.copyList[s].direction) { // 数据进行过修改
                        this.selectArray.push(this.list[i])
                    }
                }
            }
        }
        let marArray = [];
        for(let i = 0; i < this.selectArray.length; i ++) {
            let obj = {
                operateId: this.selectArray[i].guid,
                checed: this.selectArray[i].status === 'right'? false: true
            };
            marArray.push(obj)
        }


        let jsonData = {
            list: marArray,
            roleId: this.roleGuid
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.batchAddOperators ,jsonData)
            .subscribe(
                (val) => {
                    this.roleAddModal = false;
                    this.nznot.create('success', val.msg , val.msg);
                    this.getRoleEmpData();
                } ,
                (error) => {
                    this.nznot.create('error', error.code,error.msg);
                    this.roleAddModal = false;
                    this.getRoleEmpData();

                }
            );
    }

    filterOption(inputValue, option) {
        return option.description.indexOf(inputValue) > -1;
    }

    searchpro(ret: any) {
    }

    select(ret: any) {
    }




    // 列表传入的翻页数据
    monitorHandler(event) {
        this.pageIndex = event;
        this.getRoleEmpData();
    }

    // 接受子组件删除的数据 单条还是多条
    deleatData(event) {

    }


    // 列表按钮方法
    buttonDataHandler(event) {
        if (event.value ===  'Authority') {
        }

        if (event.value ===  'Overview') {
        }

    }



    selectedRow(event) { // 选中方法，折叠层按钮显示方法
    }



    // 处理行为代码，跳转、弹出框、其他交互
    isActive(event) {


        // 路由跳转
        this.router.navigate(['APPlication'],{ queryParams: { name: event } });
    }


    objJson = {};
    // 搜索框
    search() {
        this.objJson = this.operators;
        this.getRoleEmpData(this.operators);
    }

    reset() {
        this.operators = new OperaRoleModule();
        this.objJson = {};
        this.getRoleEmpData();
    }












    /*---------------------------------------------角色功能权限-----------------------------------------------------*/
    treeRoleFuncdata: any[]; // tree组件数据
/*    searchTitle: string; // 树搜索框文字
    treemenus: MenuItem[];*/
    jurisdictionTree: any[]; // 树节点保存
    checked = false
    ActiveShow = false; // 默认是false
    inputSearch = true;
    noMenu  = true; // 没有右击
    funcInfo: any; // 功能的信息
    isDisabledButton = true; // 默认都是禁选的
    isSaveEdit = true; // 修改和保存二选一按钮, 默认是修改按钮
    roleCode: string; // 角色信息
    roleTxt: any[]; // 角色信息

    checkOptionsOne: any; // 验证行为权限的数组
    nocheckOptionsOne: any; // 不验证行为权限的数组

    InitRoleFunc() {
        this.roleGuid = this.id; // 拿到父组件传过来的组织机构的guid来进行操作
        if(this.isroot) {// 代表是不存在的
            this.isrootChild = false;
        } else {
            this.isrootChild = true;
        }
        this.queryRoleData(this.roleGuid);
        this.initRoleFuncData(); // 初始化树节点
    }


    // 根据guid查询角色消息
    queryRoleData(id) {
        this.utilityService.getData(appConfig.serverUrl +  appConfig.API.queryRoleById + '/' + id)
            .subscribe(
                (val) => {
                    this.roleName = val.result.roleName;
                    console.log(this.roleName)
                    this.roleCode = val.result.roleCode;
                    this.reuseTabService.title = this.roleName + '下功能权限'
                });
    }

    initRoleFuncData() {
        this.treeRoleFuncdata = [ // 默认根节点
            {
                'label': '应用列表',
                'data': 'default Role',
                'guid': 'appRoot',
                'expandedIcon': '',
                'collapsedIcon': '',
                'children': [{}]
            }
        ];
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.queryAppByRole + '/' + this.roleGuid)
            .subscribe(
                (val) => {
                    for( var i = 0; i < val.result.allData.length; i ++) {
                        val.result.allData[i].label = val.result.allData[i].appName;
                        val.result.allData[i].children = [{}];
                    }
                    this.treeRoleFuncdata[0].children = val.result.allData; // 绑定树节点

                });
        this.treeRoleFuncdata.forEach( node => {
            this.expandFuncRecursive(node, true);
        });
    }


    getQuery(event) {
        if(event.guid !== 'appRoot') {
            let objJson = {
                appId: event.guid,
                roleId: this.roleGuid
            }
            this.utilityService.postData(appConfig.serverUrl +  appConfig.API.queryFuncBehaveByRolApp, objJson)
                .subscribe(
                    (val) => {
                        for(var i = 0; i < val.result.length; i ++) {
                            val.result[i].label = val.result[i].funcName;
                            for(var i = 0; i < val.result.length; i ++) {
                                val.result[i].label = val.result[i].funcName;
                                if (val.result[i].exit === 'true') { // true代表一个行为没有选择，没有配置过行为
                                    // val.result[i].collapsedIcon = 'fa fa-close';
                                } else { // 代表已经有了，那么对勾进行展示
                                    val.result[i].collapsedIcon = 'fa fa-check';
                                }
                            }
                        }
                        event.children = val.result; // 绑定树节点
                    });
        }
    }
    // 点击下拉的方法
    UnRolefuncfold(event) {
        this.roleTxt = [];
        this.getQuery(event.node)
    }


    // 判断是否全部选中，如果全部选中，那么全选按钮选中，反之 不选中
    checkAdult(age) {
        return age.exit === 'false'; // 判断是否全部是false
    }


    // 树节点左击事件
    TreeRoleFuncSelect(event) {
        this.roleTxt = [];
        if (event.node.childs) { // 如果存在，代表功能
            this.ActiveShow = true;
            this.funcInfo = event.node;
            if(event.node.childs.every(this.checkAdult)) { // 利用every属性，判断是否全部选中，如果全部选中，全选按钮为true
                this.checked = true;
            } else {
                this.checked = false;
            }
            let noArray = []; // 不验证行为数组
            let Array = []; // 验证行为数组
            for(var i = 0 ; i < event.node.childs.length; i ++) {
                event.node.childs[i].label = event.node.childs[i].funcName
                event.node.childs[i].guid = event.node.childs[i].guid;
                if(event.node.childs[i].exit === 'true') { // 如果是true字符串，代表是没有勾选
                    event.node.childs[i].checked = false;
                } else {
                    event.node.childs[i].checked = true;
                }
            }
            Array = _.clone(event.node.childs); // 深拷贝一份
            this.nocheckOptionsOne = _.remove(Array, function(n) { // 不需要验证权限的
                return n.ischeck === '否';
            });
            for(let  s = 0 ; s <　this.nocheckOptionsOne.length; s ++ ) {
                this.nocheckOptionsOne[s].checked = true; // 默认选中
            }
            this.checkOptionsOne = Array; // 需要验证权限的
            this.RoleFuncreset(event.node.childs); // 对所有行为进行修改
        } else {
            this.ActiveShow = false;
        }
    }


    // 全选按钮
    allSelect($event) {
        this.roleTxt = [];
        if($event) {
            for(let i = 0; i < this.checkOptionsOne.length; i ++) {
                this.checkOptionsOne[i].checked = true;
                this.checkOptionsOne[i].exit = false; // 勾选了
            }
        } else {
            for(let i = 0; i < this.checkOptionsOne.length; i ++) {
                this.checkOptionsOne[i].checked = false;
                this.checkOptionsOne[i].exit = true; // 没有勾选
            }
        }
        this.RoleFuncreset(this.checkOptionsOne);
    }


    RoleFuncreset(checkOptionsOne){
        this.roleTxt = [];
        for(var i = 0 ; i < checkOptionsOne.length; i ++) {
            if (checkOptionsOne[i].checked === true) { // 如果是true字符串，代表是没有勾选
                this.roleTxt.push(checkOptionsOne[i].funcDesc);
            }
        }
    }

    // 树节点搜索框的内容
    searchaValue() {
        const change = document.getElementById( 'searchaVals');
        const change$ = Observable.fromEvent(change, 'keyup').pluck('target', 'value').debounceTime(600);
        change$.subscribe(sussess  => {
            let jsonObj = {
                guid: 'null',
                name: sussess,
                roleId: this.roleGuid
            }

            if(sussess === '') {
                this.initRoleFuncData();
                this.ActiveShow = false;
            } else {
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.getFilterFuncBehaveByRole, jsonObj)
                    .subscribe(
                        (val) => {
                            this.ActiveShow = false;
                            if (val.code === '200' || val.code === 200) {
                                this.treeRoleFuncdata = [ // 默认根节点
                                    {
                                        'label': '应用列表',
                                        'data': 'default Role',
                                        'guid': 'appRoot',
                                        'expandedIcon': '',
                                        'collapsedIcon': '',
                                        'children': [{}]
                                    }
                                ];
                                this.treeRoleFuncdata[0].children = val.result.allData;
                                for(let i = 0; i < val.result.allData.length; i ++) {
                                    val.result.allData[i].label = val.result.allData[i].appName;
                                    if(val.result.allData[i].funcList.length > 0) { // 说明有功能
                                        for(let s = 0; s < val.result.allData[i].funcList.length; s++) {
                                            val.result.allData[i].funcList[s].label = val.result.allData[i].funcList[s].funcName;
                                        }
                                        val.result.allData[i].children = val.result.allData[i].funcList;
                                    }
                                }
                                // 展开所有的
                                this.treeRoleFuncdata.forEach( node => {
                                    this.expandFuncRecursive(node, true);
                                } );
                            }
                        },
                    );
            }

        });
    }


    // 展开所有树节点的方法
    private expandFuncRecursive(node: TreeNode, isExpand:boolean){
        node.expanded = isExpand;
        if(node.children){
            node.children.forEach( childNode => {
                this.expandFuncRecursive(childNode, isExpand);
            } );
        }
    }


    // 修改按钮展现
    edit() {
        this.isSaveEdit = false; // 保存按钮显示,修改按钮隐藏
        this.isDisabledButton = false; // 改成可以修改
    }


    RoleFuncsave() {
        this.isSaveEdit = true; // 修改按钮显示,保存按钮隐藏
        this.isDisabledButton = true; // 改成不可以修改
        let array = [];
        let treeArray = this.treeRoleFuncdata[0].children;
        for(let i = 0; i < treeArray.length; i++) { // 所有应用
            for(let j = 0; j < treeArray[i].children.length; j++) { // 所有功能
                if(treeArray[i].children[j].funcCode) {
                    for(let s = 0 ; s < treeArray[i].children[j].childs.length; s ++) {
                        if(treeArray[i].children[j].childs[s].childs === '') {
                            if (treeArray[i].children[j].childs[s].checked || treeArray[i].children[j].childs[s].exit === 'false') {
                                array.push(treeArray[i].children[j].childs[s].guid)
                            }
                        }
                    }
                }
            }
        }
        let objJson = {
            behaveIdList: array,
            roleId: this.roleGuid
        }
        this.utilityService.postData(appConfig.serverUrl +  appConfig.API.batchAdd, objJson)
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.msg , val.msg);
                },
                (error)=> {
                    this.nznot.create('error', error.msg , error.msg);
                });
    }

    cloneArray = [];

    // 進入
    changes(event) {
        if(this.checkOptionsOne.every(item => item.checked ===  true)) { // 利用every属性，判断是否全部选中，如果全部选中，全选按钮为true
            this.checked = true;
        } else {
            this.checked = false;
        }
        event.forEach(item => {
            item.exit = !item.checked + '';
        })
    }

    // 树节点三个按钮方法
    clickRoleFunc($event) {
        if($event === 'reset') {
            this.initRoleFuncData();
            this.ActiveShow = false;
        } else if($event === 'expand') { // 全部展开方法
            let treeList = this.treeRoleFuncdata[0].children
            for( let i = 0; i　< treeList.length; i++) {
                this.getQuery(treeList[i])
            }
            // 展开所有的
            this.treeRoleFuncdata.forEach( node => {
                this.expandRecursive(node, true);
            });
        } else { // 收缩
            let treeList = this.treeRoleFuncdata[0].children
            for( let i = 0; i　< treeList.length; i++) {
                this.getQuery(treeList[i])
            }
            this.ActiveShow  = false;
            this.treeRoleFuncdata.forEach( node => {
                this.expandRecursive(node, false);
            } );
        }
    }

    selectRolefuncAll(items, array) {
        this.roleTxt = [];
        if (array.every(item => item.checked ===  true)) { // 利用every属性，判断是否全部选中，如果全部选中，全选按钮为true
            this.checked = true;
        } else {
            this.checked = false;
        }
        array.forEach(item => {
            item.exit = !item.checked + '';
        })
        this.RoleFuncreset(this.checkOptionsOne);
        let obj = {
            exit: items.exit,
            appGuid: items.guidApp,
            funcGuid: items.guid,
            fromRole: items.fromRole
        }
        let index = _.findIndex(this.selectArray, function(o) { return o.funcGuid === obj.funcGuid; })
        index  ===  -1 ? this.selectArray.push(obj) : this.selectArray.splice(index, 1);
    }


    roleFunccancel() {
        this.isSaveEdit = true; // 保存按钮显示,修改按钮隐藏
        this.isDisabledButton = true; // 改成可以修改
    }

}
